package com.zzp.basepresenter.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.zzp.basepresenter.R;


public class ToastUtils {

    private static ToastUtils instance;
    private static Toast toast;

    public static ToastUtils getInstance() {
        if (instance == null) {
            synchronized (ToastUtils.class) {
                if (instance == null) {
                    instance = new ToastUtils();
                }
            }
        }
        return instance;
    }

    //带图片的 吐司
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void ToastWithImage(Context context, int imgID, String message) {
        createImageToast(context, imgID, message);
    }

    public void LongToast(Context context, int resID) {
        ShortToast(context, context.getString(resID));
    }

    public void LongToast(Context context, String text) {
        createTextToast(context, text, true);
    }

    public void ShortToast(Context context, int resID) {
        ShortToast(context, context.getString(resID));
    }

    public void ShortToast(Context context, String text) {
        createTextToast(context, text, false);
    }

    @SuppressLint("InflateParams")
    private void createTextToast(Context context, String text, boolean longShow) {
        try {
            if (context == null) {
                return;
            }
            View toastRoot = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_toast_default, null, false);
            TextView message = toastRoot.findViewById(R.id.toast_info);
            message.setText(text);
            if (toast != null) {
                toast.cancel();
                toast = null;
            }
            toast = new Toast(context.getApplicationContext());
            toast.setView(toastRoot);
            toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, (int) context.getResources().getDimension(R.dimen.y60));
            if (longShow) {
                toast.setDuration(Toast.LENGTH_LONG);
            } else {
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void createImageToast(Context context, int imgID, String message) {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
        toast = Toast.makeText(context.getApplicationContext(),
                message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        if (imgID != 0) {
            LinearLayout toastView = (LinearLayout) toast.getView();
            ImageView imageCodeProject = new ImageView(context.getApplicationContext());
            imageCodeProject.setImageResource(imgID);
            toastView.addView(imageCodeProject, 0);
            toastView.setBackground(context.getApplicationContext().getDrawable(R.drawable.style_t80_solid_layout));
        }
        toast.show();
    }
}
